(function(){
	// 页面初始加载订单工序 是否加急
	$J("#process_sel").append(selectHtml.orderProcess());
	$J("#isExpedited_sel").append(selectHtml.orderIsExpedited());
	
	$J("#addGoods").click(function() {
		var goodsid = $J("#orderid").val() + "_" + ($J("#o_create_goods_list .o_goods_detail").length + 1);
		var html = '<ul class="o_goods_detail">'+
				'<li>'+
					'<h3>编号</h3>'+
					'<a href="javascript:void(0);"><span class="delIcon"></span></a>'+
				'</li>'+
				'<li>'+
					'<label>商品编号</label>'+
					'<input class="ui_input" data_name="id" type="text" value="0" style="display: none;">'+
					'<input class="ui_input" data_name="isRemove" type="text" value="0" style="display: none;">'+
					'<input class="ui_input" data_name="goodsid" type="text" value="' + goodsid + '" disabled placeholder="商品编号">'+
				'</li>'+
				'<li>'+
					'<h3>商品详情</h3>'+
				'</li>'+
				'<li>'+
					'<label>材质</label>'+
					'<input class="ui_input" data_name="goodsmaterial" type="text" placeholder="商品材质" validate="required">'+
				'</li>'+
				'<li>'+
					'<label>颜色</label>'+
					'<input class="ui_input" data_name="goodscolor" type="text" placeholder="商品颜色" validate="required">'+
				'</li>'+
				'<li>'+
					'<label>款式</label>'+
					'<input class="ui_input" data_name="goodsstyle" type="text" placeholder="商品款式" validate="required">'+
				'</li>'+
				'<li>'+
					'<label>数量</label>'+
					'<input class="ui_input" data_name="amount" type="text" placeholder="商品数量" validate="required">'+
				'</li>'+
				'<li>'+
					'<label>单价</label>'+
					'<input class="ui_input" data_name="price" type="text" placeholder="商品单价" validate="required">'+
				'</li>'+
				'<li>'+
					'<label>工序</label>'+ selectHtml.goodsProcess(0) +
				'</li>'+
				'<li>'+
					'<label>类型</label>'+ selectHtml.goodsType(0) +
				'</li>'+
				'<li>'+
					'<label>尺寸</label>'+
					'<input class="ui_input" data_name="goodssize" type="text" style="margin-right: 4px;" placeholder="尺寸" validate="required">'+
				'</li>'+
				'<li>'+
					'<label>图片</label>'+
					'<input class="ui_input" data_name="goodspic" type="text" style="margin-right: 4px;" placeholder="图片">'+
				'</li>'+
				'<li>'+
					'<label>备注</label>'+
					'<textarea class="ui_input" data_name="remark" rows="4" placeholder="备注"></textarea>'+
				'</li>'+
			'</ul>';
		var goodsDetail = $J("#o_create_goods_list").append(html);
		goodsDetail.find("div[data_name='process']").selOp();
		goodsDetail.find("div[data_name='goodstype']").selOp();
	});
	
	$J("#o_create_goods_list .delIcon").live('click', function() {
		delGoods($J(this));
	});
	
	$J("#saveBtn").click(function(){
		save();
	});
	
	$J("#cancelBtn").click(function(){
		modifyOrderCancel();
	});
	
	$J(".d_sel").selOp();
	
	$J('#begintime').DatePicker({
		eventName: 'focus',
		date: $J('#begintime').val(),
		current: $J('#begintime').val(),
		starts: 1,
		position: 'bottom',
		onBeforeShow: function(){
			$J('#endtime').DatePickerHide();
			$J('#begintime').DatePickerSetDate($J('#begintime').val(), true);
		},
		onChange: function(formated, dates){
			$J('#begintime').val(formated);
			$J('#begintime').DatePickerHide();
			$J('#begintime').blur();
		}
	});
	
	$J('#endtime').DatePicker({
		eventName: 'focus',
		date: $J('#endtime').val(),
		current: $J('#endtime').val(),
		starts: 1,
		position: 'bottom',
		onBeforeShow: function(){
			$J('#begintime').DatePickerHide();
			$J('#endtime').DatePickerSetDate($J('#endtime').val(), true);
		},
		onChange: function(formated, dates){
			$J('#endtime').val(formated);
			$J('#endtime').DatePickerHide();
			$J('#endtime').blur();
		}
	});
	
	function save() {
		// 输入规则验证
		var validate = $J("#ordersInfo").validate();
		if (!validate) {
			return false;
		}
		modifyOrderSave(saveParam());
	}
	
	function saveParam() {
		var orderkeyid = $J("#orderkeyid").val();
		var orderid = $J("#orderid").val();
		var agents = $J("#agents").val();
		var customername = $J("#customername").val();
		var customerphone = $J("#customerphone").val();
		var customeraddress = $J("#customeraddress").val();
		var paidmoney = $J("#paidmoney").val();
		var allmoney = $J("#allmoney").val();
		var process = $J("#process_sel span.d_sel_text").attr("data_val");
		var isexpedited = $J("#isExpedited_sel span.d_sel_text").attr("data_val");
		var begintime = $J("#begintime").val();
		var endtime = $J("#endtime").val();
		var remark = $J("#remark").val();
		var goodsList = [];

		$J("#o_create_goods_list .o_goods_detail").each(function(i){
			var goodskeyid = $J(this).find("input[data_name='id']").val();
			var goodsid = $J(this).find("input[data_name='goodsid']").val();
			var goodsmaterial = $J(this).find("input[data_name='goodsmaterial']").val();
			var goodscolor = $J(this).find("input[data_name='goodscolor']").val();
			var goodsstyle = $J(this).find("input[data_name='goodsstyle']").val();
			var amount = $J(this).find("input[data_name='amount']").val();
			var price = $J(this).find("input[data_name='price']").val();
			var process = $J(this).find("div[data_name='process']").find("span.d_sel_text").attr("data_val");
			var goodstype = $J(this).find("div[data_name='goodstype']").find("span.d_sel_text").attr("data_val");
			var goodssize = $J(this).find("input[data_name='goodssize']").val();
			var goodspic = $J(this).find("input[data_name='goodspic']").val();
			var remark = $J(this).find("textarea[data_name='remark']").val();
			var isRemove = $J(this).find("input[data_name='isRemove']").val();
			
			goodsList.push({
				"id" : goodskeyid,
				"goodsid" : goodsid,
				"goodsmaterial" : goodsmaterial,
				"goodscolor" : goodscolor,
				"goodsstyle" : goodsstyle,
				"amount" : amount,
				"price" : price,
				"process" : process,
				"goodstype" : goodstype,
				"goodssize" : goodssize,
				"goodspic" : goodspic,
				"remark" : remark,
				"isRemove" : isRemove
			});
		});
		
		return {
			"id" : orderkeyid,
			"orderid" : orderid,
			"agents" : agents,
			"customername" : customername,
			"customerphone" : customerphone,
			"customeraddress" : customeraddress,
			"paidmoney" : paidmoney,
			"allmoney" : allmoney,
			"isexpedited" : isexpedited,
			"process" : process,
			"begintime" : begintime,
			"endtime" : endtime,
			"remark" : remark,
			"goodsList" : JSON.stringify(goodsList)
		};
	}
})();